package com.cloudm.technician.ui.modules.cloudm_box.change_box

import com.cloudm.technician.data.network.CloudmBoxApi
import com.cloudm.technician.extensions.isSnCode
import com.cloudm.technician.ui.base.RxPresenter

/**
 * 作者: wesley
 * 时间: 2017/12/18
 * 描述: 更换盒子业务逻辑
 */
class ChangeBoxPresenter(private val view: ChangeBoxContract.View,
                         private val cloudmBoxApi: CloudmBoxApi) : RxPresenter(),
        ChangeBoxContract.Presenter {

    override fun getChangeBoxListInfo(page: Int, size: Int, status: Int) {
        request(cloudmBoxApi.getChangeBoxListInfo(page, size, status), view,
                onSuccess = { list, loadMoreInfo ->
                    when (list) {
                        null -> view.loadSuccess(list, loadMoreInfo)
                        else -> {
                            for (i in 0 until list.size) {
                                list[i].id = list[i].deviceId
                                list[i].createTime = list[i].gmtCreate
                            }
                            view.loadSuccess(list, loadMoreInfo)
                        }
                    }
                }, onFailure = { errorMsg ->
            view.loadFailure(errorMsg)
        })
    }

    override fun deleteDevice(recordId: String?) {
        request(cloudmBoxApi.deleteChangeBoxDevice(recordId), view, onSuccess = { _, _ ->
            view.deleteSuccess()
        }, onFailure = { errorMsg ->
            view.deleteFailure(errorMsg)
        })
    }


    override fun dealWithScanResult(sn: String?) {

        if (sn == null || sn.isNullOrEmpty()) {
            view.showSnEmpMsg()
            return
        }

        if (!sn.isSnCode()) {
            view.showSnFormatErrMsg()
            return
        }

        view.openCloudmDeviceDetailActivity(sn)

    }
}